"""
4.1.1图像反转
"""

import cv2
import numpy as np

# # 读取图像
image = cv2.imread('1.webp', cv2.IMREAD_GRAYSCALE)
if image is None:
     print("Error: Unable to load image.")
     exit()

# 图像反转
inverted_image = 255 - image

# # 显示原始图像和反转后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Inverted Image', inverted_image)
cv2.waitKey(0)
cv2.destroyAllWindows()


"""
4.1.2对数变换
"""
import cv2
import numpy as np
# # 读取图像
image = cv2.imread('1.webp', cv2.IMREAD_GRAYSCALE)
if image is None:
 print("Error: Unable to load image.")
exit()

# # 对数变换
c = 255 / np.log(1 + np.max(image))  # 归一化系数
log_transformed = c * np.log(1 + image)

# # 转换为8位无符号整数
log_transformed = np.uint8(log_transformed)

# # 显示原始图像和对数变换后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Log Transformed Image', log_transformed)
cv2.waitKey(0)
cv2.destroyAllWindows()


"""
4.1.3 幂次变换
"""
import cv2
import numpy as np
#
# # 读取图像
image = cv2.imread('1.webp', cv2.IMREAD_GRAYSCALE)
if image is None:
    print("Error: Unable to load image.")
    exit()
#
# # 幂次变换
gamma = 2.2  # 伽马值
c = 255 / (np.max(image) ** gamma)  # 归一化系数
power_transformed = c * (image ** gamma)
#
# # 转换为8位无符号整数
power_transformed = np.uint8(power_transformed)
#
# # 显示原始图像和幂次变换后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Power Transformed Image', power_transformed)
cv2.waitKey(0)
cv2.destroyAllWindows()

"""
4.1.4 线性变换
"""
import cv2
import numpy as np
#
# # 读取图像
image = cv2.imread('1.webp', cv2.IMREAD_GRAYSCALE)
if image is None:
    print("Error: Unable to load image.")
    exit()
#
# # 线性变换参数
a = 1.5  # 斜率
b = 50   # 截距
#
# # 线性变换
linear_transformed = cv2.convertScaleAbs(image, alpha=a, beta=b)
#
# # 显示原始图像和线性变换后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Linear Transformed Image', linear_transformed)
cv2.waitKey(0)
cv2.destroyAllWindows()